#include <bits/stdc++.h>
#define int long long
using namespace std;

void chmax(int& a, int b) { if (a < b) a = b; }

const int N = 200005, V = 1000005;

int n, ans, a[N];

struct node {
    int mx, delta, val[V];
    void clear() {
        mx = delta = 0;
        memset(val, -0x3f, sizeof(val));
        val[0] = 0;
    }
    int operator[](int x) { return val[x] + delta; }
    void set(int x, int y) {
        val[x] = y - delta;
        mx = max(mx, y);
    }
    void add(int x) { mx += x, delta += x; }
} f0, f1;

void solve() {
    f0.clear();
    f1.clear();
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 0; i < n; i++) {
        int t0 = max(f0.mx, f0[a[i + 1]] + a[i + 1]);
        int t1 = max(f1.mx, f1[a[i + 1]] + a[i + 1]);
        f0.add((a[i] == a[i + 1]) * a[i + 1]);
        f1.add((a[i] == a[i + 1]) * a[i + 1]);
        f1.set(a[i], max(f1[a[i]], t0));
        f0.set(a[i], max(f0[a[i]], t1));
    }
    ans = 0;
    for (int i = 0; i < V; i++) {
        chmax(ans, f0[i]);
        chmax(ans, f1[i]);
    }
    cout << ans << endl;
}

signed main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}